
package com.netease.xone.fragment;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.XoneServiceCode;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.handmark.pulltorefresh.library.X1LoadingAdapterViewBase.OnLoadingListener;
import com.netease.xone.R;
import com.netease.xone.adapter.AdviceAdapter;
import com.netease.xone.widget.listview.LoadingListView;

public class FragmentAdviceList extends FragmentBase {

    private static final int GET_MAX_COUNT = 500;
    private static final int GET_DEFAULT_COUNT = 25;
    // Views
    private LoadingListView mLoadingList;
    private AdviceAdapter mAdapter;
    // Tid
    private int mAdviceTid;

    public static FragmentAdviceList newInstance() {
        FragmentAdviceList f = new FragmentAdviceList();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        XoneService.getInstance().addListener(mCallBack);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_loading_listview, null);
        mAdapter = new AdviceAdapter(getActivity());
        mLoadingList = (LoadingListView)view;
        mLoadingList.setAdapter(mAdapter);
        mLoadingList.disablePullToRefresh();
        mLoadingList.setOnLoadingListener(new OnLoadingListener() {

            @Override
            public void onRefreshing() {

            }

            @Override
            public void onLoading() {
                mAdviceTid = XoneService.getInstance().doGetAdaviceList();
            }

            @Override
            public void onLoadingMore() {
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mLoadingList.load();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mCallBack);
    }

    private XoneCallBack mCallBack = new XoneCallBack() {

        @Override
        public void onGetAdviceList(int transactionId,
                java.util.List<protocol.meta.UserAdvice> dynamicList) {
            if (mAdviceTid == transactionId) {
                if(dynamicList == null || dynamicList.size() == 0){
                    mLoadingList.onNoContent(getString(R.string.success_advice_no_advice));
                }else{
                    mAdapter.clear();
                    mAdapter.addTail(dynamicList);
                    mLoadingList.onLoadingComplete(false);
                }
            }
        };

        @Override
        public void onGetAdviceListError(int transactionId, int errCode, String err) {
            if (mAdviceTid == transactionId) {
                if(errCode == XoneServiceCode.NETWORK_ERR_COMMON){
                    mLoadingList.onNoNetwork();
                }else{
                    mLoadingList.onLoadingError();
                }
            }
        };
    };
}
